home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
batchut
/
bathints.zip
/
BH_10.DOC
< prev
next >
Wrap
Text File
|
1988-10-16
|
14KB
|
363 lines
BAT-HINT # 10
**************************************************************************
from the BATHINTS library... part of the BATPOWER CONFERENCE from:
THE PAINFRAME OPUS/FIDO 261/1004
Baltimore, Maryland 1-301-488-7461
**************************************************************************
The IF subcommand
The IF subcommand is not unique to batch file processing in DOS... the
command may be issued from either a batch file or from the command line,
though it finds its greatest use from within the batch file. Through the
use of IF a user may check for several things:
1. whether a file exists
2. whether a specific errorlevel has been returned from the keyboard
or from a program
3. whether a variable exists
4. the value of a variable or string
The general syntax of the IF command is:
if [not] {true statement} {perform command}
where [not] is optional, {true statement} is a legal statement to test with
the IF command and {perform command} is any executable command.
Errorlevel Testing
------------------
Many of you have no doubt already used the IF command to test for
errorlevels, so lets start with a simple example of an errorlevel test:
sample1.bat
-----------
echo off
cls
:start
echo Press a key... (you must press the right one!)
key
if errorlevel 65 goto correct
echo Sorry, that was the wrong key...
goto start
:correct
echo You pressed uppercase A
echo That was the letter I was waiting for!
This example uses the KEY.COM utility available from BATPOWER.ARC, a
utility that pauses the execution of a batch file and waits for a single
key to be pressed at the keyboard... and then returns an errorlevel code
equal to the scan code of the key pressed (all keys and key combinations
have an associated scan code used by DOS; for details on the specific scan
codes see BATHINT ? ?... bh_?.doc; for details on the use of KEY.COM see
the doc file KEY.DOC packaged with the program in BATPOWER.ARC). In this
example, the IF command is waiting for the uppercase (capital) A to be
pressed... its scan code is 65. Lets examine the command in more detail:
if errorlevel 65 goto correct
We can divide the command statement into three parts according to the
syntax already given. First, IF, well thats what we are talking about.
Second, errorlevel 65, that is the {true statement} that we are testing...
is the errorlevel 65, or more precisely, is the errorlevel equal to or
greater than 65? Thats the way testing for errorlevels works. If a
statement is made like:
if errorlevel 65 {perform command}
then the IF command tests whether the errorlevel returned is equal to or
greater than the specified number. In sample1.bat we could narrow down the
specificity of the test with a statement like:
if errorlevel 65 if not errorlevel 66 {perform command}
AhAh! A nested IF command. Examine closely what this statement says: If
the errorlevel is 65 or greater AND if the errorlevel is NOT 66 or greater
then {perform command}... so the errorlevel must be equal to 65 (or more)
but not equal to 66 (or more)... thus the errorlevel MUST be 65 for the
statement to be true. If the statement is true (if an A was pressed) then
{perform command}, which in sample1.bat is GOTO CORRECT, is executed. If
it is false (if the errorlevel is less than 65) then {perform command} is
not executed and the batch file ignores {perform command} and jumps to the
next line ( the line below). So with "if errorlevel XX if not errorlevel
XX+1" we may single out a specific errorlevel for testing. Examining the
third part of the statement "goto correct", we see the command to be
executed IF THE STATEMENT IS TRUE. GOTO is batch file specific command in
DOS which instructs DOS to jump to a specified label. In our example, the
label is "correct" (though it could be anything, so long as that label
exists). Notice that further down in the batch file is the following line:
:correct
and notice also that when specifying a label with the GOTO command it is
not necessary to include the colon (:) before the label (but the label must
have a colon where it actually appears as a directive in the batch file).
So if everything is true (you actually did press the letter A), sample1.bat
performs the "GOTO CORRECT" command and searches the batch file for the
label :correct then continues executing each line, one at a time, from the
label :correct. In our example, two echo commands are executed indicating
that that was the right key to press... and the batch file ends. If the
statement was FALSE (you did not press A; the errorlevel was less than and
not equal to 65) then the batch file ignores the "goto correct" command and
skips to the next line, which echos that you pressed the wrong key and then
performs the "goto start" command, which jumps back to the :start label and
asks you to press a key again (the letter A!).
So great eh? What can I do with it? Well there are many applications,
only one of which is a small menu of possible selections. As a last
example, consider the following scenerio: You have three programs from
which to choose and you have written a batch file which starts each
application by its name. The applications are: Yorgi (a fanatstic game of
fun and skill?!), RipWrite (a great word processor) and Snarf (a utility
program that you cannot live without). Lets assume that each batch file
(yorgi.bat, ripwrite.bat and snarf.bat) are available through the normal
DOS command line by virtue of the fact that they are in the c:\bat
subdirectory that is named in your path command... so that they be found
when called upon). Rather than type the name of the batch file which
starts these great programs, you have created a simple menu from which with
a single keystroke you may select any one of them. That batch file may be
written like this:
menu.bat
--------
echo off
cls
echo Main Menu:
echo 1. Yorgi
echo 2. RipWrite
echo 3. Snarf
echo Press a number according to your selection...
key
if errorlevel 51 if not errorlevel 52 goto select3
if errorlevel 50 if not errorlevel 51 goto select2
if errorlevel 49 if not errorlevel 50 goto select1
goto sorry
:select1
yorgi
:select2
ripwrite
:select3
snarf
:sorry
echo You must press a number between 1 and 3
pause
goto start
cls
Notice that the IF ERRORLEVEL statements are numbered backwards from the
scan code for number 3 (51) to the scan code for number 1 (49). Following
the logic given for sample1.bat, you should be able to see that you must
start from the highest number down (if you have questions just ask!!!).
Testing for the Presence of a File
----------------------------------
IF can test for the existance of a file. Examine the following batch
file:
sample2.bat
-----------
echo off
cls
if exist myword.doc echo myword.doc exists!
cls
Simple enough... sample2.bat tests for the existance of a file called
"myword.doc" and if the statement is true (that is, if myword.doc does
exist), it executes the command "echo myword.doc exists!". If the file is
not found the remainder of the command line is ignored and the batch file
continues processing at the next line, which in this example clears the
screen. Note that the "if exist" command only recognizes files in the
current directory. To test for a file located elsewhere you should log
onto the drive and into the subdirectory where the file is presumed to
exist. For example, if the file was located on drive B in a subirectory
called "document", you would need to change sample2.bat to read:
echo off
cls
b:
cd\document
if exist b:\document\myword.doc echo myword.doc exists!
Occasionally, the test for a specific